<?php
// +----------------------------------------------------------------------
// |物流管理控制器
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use app\common\Model\ConfigModel;
use app\admin\Model\CountryModel;
use app\admin\system\LogisticsService;
use think\facade\Db;
use think\Request;
use app\common\Model\StroageModel;


class Logistics extends BaseController
{
    //快递列表
    public function index()
    {
        //物流商的信息
        $field = 'id,name_Cn';//要显示的字段       
        $where = [];//查询条件
        $logistice_providerist = $this->infos(Db::name('logistice_provider'),$field,$where);
        $logistice_providerist=array_column($logistice_providerist,null,'id');
        $where[]=['status','=',1];//查询条件仓     
        $logistice_providerist_isu = $this->infos(Db::name('logistice_provider'),$field,$where);
       
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::getList($param['params']);   
            if($result['list']){
                foreach ($result['list'] as $key => $value) {   
                    $result['list'][$key]['provider_name'] = $logistice_providerist[$value['p_id']]['name_Cn'];
                }
            }
            success($result);
        }else{
            view::assign('logistice_providerist_isu',json_encode($logistice_providerist_isu));
            view::assign('logistice_providerist',json_encode($logistice_providerist));
            return View::fetch();
        }
    }

     //快递新增或者修改
     public function create(){
        if(request()->isPost()){
            $param = request()->param(); 
            $result = LogisticsService::create( $this->trimArrayValues($param['params']));   
            success($result);
        }else{
            return View::fetch();
        }
     }

    //快递服务商列表
    public function provider(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::getproviderList($this->trimArrayValues($param['params']));   
            success($result);
        }else{
            return View::fetch();
        }
    }

      //快递服务商新增或者修改
      public function provider_create(){
        if(request()->isPost()){
            $param = request()->param(); 
            $result = LogisticsService::provider_create($this->trimArrayValues($param['params']));   
            success($result);
        }else{
            return View::fetch();
        }
    }


    //快递资费的列表
    public function charge(){
        //物流商的信息
        $field = 'id,name_Cn';//要显示的字段       
        $where = [];//查询条件
        $logistice_providerist = $this->infos(Db::name('logistice_provider'),$field,$where);
        $logistice_providerist=array_column($logistice_providerist,null,'id');  
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::charge($param['params']);
            success($result);
        }else{       
            //获取国家
            $countrys_list=(new CountryModel())->getcountryAllList();             
            view::assign('countrys',json_encode($countrys_list));
            view::assign('provider',json_encode($logistice_providerist));
            return View::fetch();
        }
    }
    

    //通过物流商id查询物流渠道
    public function channel_list(){      
    
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::channel_list($param['params']);          
            success($result);
        }
    }
    //快递资费新增或者修改   
    public function  charge_create(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::charge_create($param['params']);          
            success($result);
        }
    }
    //快递资费删除
    public function charge_detele(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::charge_detele($param['params']);          
            success($result);
        }
    }

    //快递规则的列表
    public function rule(){
        //仓库的信息
        $Model=new StroageModel();
         $storagelist =  $Model->getActivelist();  
          //物流商的信息
        $field = 'id,name_Cn';//要显示的字段       
        $where = [];//查询条件        
        $where[]=['status','=',1];//查询条件仓     
        $logistice_providerist_isu = $this->infos(Db::name('logistice_provider'),$field,$where);
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::rule($param['params'],$logistice_providerist_isu);
            success($result);
        }else{       
            $config_list=(new ConfigModel())->getConfigist('platform');           
           
             view::assign('storagelist',json_encode($storagelist));
             view::assign('provider',json_encode($logistice_providerist_isu));
             view::assign('platform',json_encode(explode(',',$config_list)));
            return View::fetch();
        }
    }

   //快递规则的添加与修改
    public function  rule_create(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::rule_create($param['params']);
            success($result);
        }
    }
   //快递规则的删除
    public function rule_delete(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::rule_delete($param['params']);
            success($result);
        }
    }


    //运费模板的列表
    public function  logistice_template(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::logistice_template($param);
            success($result);
        }else{
            return View::fetch();
        }
    }
     //通过店铺id查询运费模板
    public function  logistice_template_list(){
     if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::logistice_template_list($param);
            success($result);
        }
    }


    //运费测算
    public function  freighttrial(){
        if(request()->isPost()){
            $param = request()->param();          
            $result = LogisticsService::freighttrial($param);
            success($result);
        }else{
              //获取国家
            $countrys_list=(new CountryModel())->getcountryAllList();             
            view::assign('countrys',json_encode($countrys_list));
            return View::fetch();
        }
    }

}